package day13;

import org.junit.Test;

import java.util.*;


public class Demo3 {
    List<Integer> list = new ArrayList();

    @Test
    public void print() {
        list.add(1);
        list.add(10);
        list.add(3);
        list.add(4);
        list.add(5);
        System.out.println(list);
        //Collections.sort(list);//升序
        //Comparator com = (o1,o2) -> o2-o1;
        Collections.sort(list,(o1,o2) -> o2-o1);//降序
        //Collections.sort(list);
        System.out.println(list);
    }

    @Test
    public void sortString() {
        List<String> list1 = new ArrayList<>();
        list1.add("abc");
        list1.add("def");
        list1.add("ghi");
        list1.add("jkl");
        list1.add("mno");
        System.out.println(list1);
        Collections.sort(list1,(o1,o2) -> o2.compareTo(o1));
        System.out.println(list1);
    }

    @Test
    public void sortList() {
        List<Employee> list=new ArrayList<>();
        list.add(new Employee(100,"志广",8000));
        list.add(new Employee(300,"万鑫",8000));
        list.add(new Employee(200,"万聪",10000));
        list.add(new Employee(400,"金科",10000));
        list.add(new Employee(500,"伊杰",6000));
        //排序规则: 先根据工资降序排列 工资相同按照员工编号升序排列
        Collections.sort(list,(o1,o2)->{
            int n=(int)(o2.sal-o1.sal);
            if(n==0){
                n=o1.empno-o2.empno;
            }
            return n;
        });
        for(Employee e:list){
            System.out.println(e.empno+" "+e.name+" "+e.sal);
        }
    }
}


    class Employee{
        int empno;
        String name;
        double sal;
        public Employee(int empno, String name, double sal) {
            this.empno = empno;
            this.name = name;
            this.sal = sal;
        }
    }
